package com.example.sport.utils;

/**
 * @projectName: travel
 * @package: com.example.travel.utils
 * @className: JwtUtil
 * @author: Liqian
 * @description: TODO
 * @date: 2024/4/12 16:32
 * @version: 1.0
 */

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;


public class JwtUtil {

    /**
     * 有效期为 60 * 60 *1000  一个小时
     */
    public static final Long JWT_TTL = 60 * 60 * 1000 * 24L;

    /**
     * 设置秘钥明文
     */
    public static final String JWT_KEY = "jwt_travel";
    public static final String JWT_CLAIMS_NAME = "jwt_claims";

    public static String genToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim(JWT_CLAIMS_NAME, claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + JWT_TTL))//过期时间
                .sign(Algorithm.HMAC256(JWT_KEY));
    }

    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(JWT_KEY))
                .build()
                .verify(token)
                .getClaim(JWT_CLAIMS_NAME)
                .asMap();
    }


}
